package cn.neusoft.controller;

import cn.neusoft.domain.Order;
import cn.neusoft.domain.Substation;
import cn.neusoft.exception.AppException;
import cn.neusoft.exception.AppExceptionCodeMsg;
import cn.neusoft.service.OrderService;
import cn.neusoft.vo.ResultBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.mockito.internal.matchers.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/order")
@Api(value = "订单模块的API",description="订单相关的CRUD功能")
public class OrderController {

    @Autowired
    OrderService orderService;

    @GetMapping("/all")
    @ApiOperation(value = "查询所有的订单")
    public ResultBean findAll(){
        List<Order> re= orderService.findAll();
        return ResultBean.success(re);
    }


    public ResultBean add(Order order){
        if(order==null){
            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }

        orderService.add(order);
        return ResultBean.success();
    }
    @GetMapping("/status/{status}")
    @ApiOperation(value = "根据订单状态查找")
    public  ResultBean findByStatus(@PathVariable("status") String status){
        if(status == ""){
//            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
            List<Order>re = orderService.findAll();
            return ResultBean.success(re);
        }
        List<Order>re = orderService.findByStatus(status);
        return ResultBean.success(re);
    }

    @GetMapping("/type/{status}")
    @ApiOperation(value = "根据订单种类查找")
    public  ResultBean findByType(@PathVariable("status") String status){
        if(status == ""){
//            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
            List<Order>re = orderService.findAll();
            return ResultBean.success(re);
        }
        List<Order>re = orderService.findByType(status);
        return ResultBean.success(re);
    }

    @GetMapping("/allfz")
    public ResultBean findAllfz(){
        List<Substation>re = orderService.findAllfz();
        return ResultBean.success(re);
    }

    @GetMapping("/cusname/{name}")
    @ApiOperation(value = "根据收货人姓名查找（带模糊")
    public  ResultBean findByReceiverName(@PathVariable("name") String name){
        if(name == ""){
//            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
            List<Order>re = orderService.findAll();
            return ResultBean.success(re);
        }
        List<Order>re = orderService.findByReceiverName(name);
        return ResultBean.success(re);
    }

    @GetMapping("/conname/{name}")
    @ApiOperation(value = "根据订货人姓名查找（带模糊")
    public  ResultBean findByConsigneeName(@PathVariable("name") String name){
        if(name == ""){
//            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
            List<Order>re = orderService.findAll();
            return ResultBean.success(re);
        }
        List<Order>re = orderService.findByConsigneeName(name);
        return ResultBean.success(re);
    }

    @GetMapping("/id/{id}")
    @ApiOperation(value = "根据订单号")
    public  ResultBean findById(@PathVariable("id") String id){
        if(id == ""){
//            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
            List<Order>re = orderService.findAll();
            return ResultBean.success(re);
        }
        List<Order>re = orderService.findById(id);
        return ResultBean.success(re);
    }
}
